package com.zhu.controller;


import com.zhu.pojo.User;
import com.zhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/updUser")
    public String upd(@RequestBody User user) {
//        System.out.println(user);
        boolean flag = userService.isModify(user);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/getUserId/{username}")
    public int getId(@PathVariable("username") String username) {
        return userService.getIdByName(username);
//        System.out.println(user);
//        boolean flag = userService.isModify(user);
//        if(flag){
//            return "success";
//        }else {
//            return "false";
//        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/getUserAll")
    public List<User> getAll() {
        return userService.userList();
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/saveUser")
    public String save(@RequestBody User user) {
        boolean flag = userService.add(user);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/delUser/{id}")
    public String upd(@PathVariable("id") int id) {
        boolean flag = userService.del(id);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/isAdmin/{username}")
    public String role(@PathVariable("username") String username) {
        return userService.role(username);

    }

}

